package org.groupg.practice.dbcp2;

import org.apache.commons.dbcp2.BasicDataSource;

import java.sql.Connection;
import java.sql.SQLException;
import java.time.Duration;

public class DBCP2Example {
    public static void main(String[] args) throws SQLException {
        // 创建数据库连接池配置
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/mydatabase");
        dataSource.setUsername("username");
        dataSource.setPassword("password");
        dataSource.setMinIdle(5);
        dataSource.setMaxIdle(10);
        dataSource.setMaxTotal(20);
        dataSource.setMaxWait(Duration.ofSeconds(10000));

        // 从连接池中获取连接
        Connection conn = dataSource.getConnection();
        // 使用连接进行数据库操作...

        // 操作完成后关闭连接
        conn.close();
    }
}